/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * spliterPanel.java
 *
 * Created on 16/Mai/2012, 16:17:21
 */
package smallsimulator;

/**
 *
 * @author Old
 */
public class spliterPanel extends javax.swing.JPanel implements panelItem {

    private FrameDocumento pai = null;
    private spliterInfo currInfo = null;
    private boolean loaded = false;
    private int myNum = 0;

    /** Creates new form spliterPanel */
    public spliterPanel(FrameDocumento p) {
        initComponents();
        pai = p;
        jSlider1.setMinimum(0);
        jSlider1.setMaximum(1000);
        jSlider1.setMinorTickSpacing(1);
        currInfo = this.toInfo();
        loaded = true;
    }

    public spliterPanel(spliterInfo infoIn, FrameDocumento p, int maxcombos) {
        initComponents();
        pai = p;
        jSlider1.setMinimum(0);
        jSlider1.setMaximum(1000);
        jSlider1.setMinorTickSpacing(1);

        this.updateMaxIndexes(maxcombos, 0);
        setValores(infoIn);
        currInfo = this.toInfo();
        loaded = true;
    }

    public void setValores(spliterInfo infoIn) {
        jComboBox1.setSelectedIndex(infoIn.getTarget1());
        jComboBox2.setSelectedIndex(infoIn.getTarget2());



        jTextField1.setText((infoIn.getChance1() * 100) + "");
        jTextField2.setText(((1 - infoIn.getChance1()) * 100) + "");
        updateBarFromVals();
        revalRepaint();
    }

    private void updateValsFromBar() {
        jTextField1.setText((jSlider1.getValue() / 10f) + "");
        jTextField2.setText(((1000 - jSlider1.getValue()) / 10f) + "");
    }

    private void updateBarFromVals() {
        jSlider1.setValue((int) (Float.parseFloat(jTextField1.getText()) * 10));
    }

    public void setNum(int num) {
        jLabel4.setText(num + "");

        myNum = num;
        revalRepaint();
    }

    public void revalRepaint() {
        this.revalidate();
        this.repaint();
    }

    public spliterInfo toInfo() {
        return new spliterInfo(jComboBox1.getSelectedIndex(), jComboBox2.getSelectedIndex(), (jSlider1.getValue() / 1000f));
    }

    public void updateMaxIndexes(int max, int idxtarget) {
        boolean shrinked = false;
        if (jComboBox1.getItemCount() == max) {
        } else {

            if (jComboBox1.getItemCount() > max) {
                shrinked = true;
            }

            int sel = jComboBox1.getSelectedIndex();
            jComboBox1.removeAllItems();
            for (int i = 0; i < max; i++) {
                jComboBox1.addItem(makeObj(i + ""));
            }
            if (sel >= max) {
                sel = max - 1;
            } else if (idxtarget < sel && shrinked) {
                sel--;
            } else if (idxtarget < sel && !shrinked) {
                sel++;
            }
            jComboBox1.setSelectedIndex(sel);
        }

        if (jComboBox2.getItemCount() == max) {
        } else {

            if (jComboBox2.getItemCount() > max) {
                shrinked = true;
            }

            int sel = jComboBox2.getSelectedIndex();
            jComboBox2.removeAllItems();
            for (int i = 0; i < max; i++) {
                jComboBox2.addItem(makeObj(i + ""));
            }
            if (sel >= max) {
                sel = max - 1;
            } else if (idxtarget < sel && shrinked) {
                sel--;
            } else if (idxtarget < sel && !shrinked) {
                sel++;
            }
            jComboBox2.setSelectedIndex(sel);
        }
    }

    private Object makeObj(final String item) {
        return new Object() {

            public String toString() {
                return item;
            }
        };
    }

    public void changed() {
        if (loaded) {
            pai.changed();
            System.out.println("changed split");
        }
    }

    private void validateAll() {
        spliterInfo tempI = null;
        try {
            String tiporand = "log";
            if (jComboBox1.getSelectedIndex() == 0) {
                tiporand = "def";
            }
            tempI = new spliterInfo(jComboBox1.getSelectedIndex(), jComboBox2.getSelectedIndex(), (jSlider1.getValue() / 1000f));

            if (!tempI.equals(currInfo)) {
                changed();
                currInfo = this.toInfo();
            }
            updateBarFromVals();
        } catch (NumberFormatException e) {
            setValores(currInfo);
        }
    }
    
    public void setSelected(boolean sel){
        if(sel){
            setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        }else {
            setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        }
        revalRepaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jSlider1 = new javax.swing.JSlider();
        jTextField2 = new javax.swing.JTextField();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(smallsimulator.Main.class).getContext().getResourceMap(spliterPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setMaximumSize(new java.awt.Dimension(270, 100));
        setMinimumSize(new java.awt.Dimension(270, 100));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(270, 100));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validateC(evt);
            }
        });
        jComboBox1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                validateCf(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1" }));
        jComboBox2.setName("jComboBox2"); // NOI18N
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validateC(evt);
            }
        });
        jComboBox2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                validateCf(evt);
            }
        });

        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validateNums(evt);
            }
        });
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                validateNumsf(evt);
            }
        });

        jSlider1.setBackground(resourceMap.getColor("jSlider1.background")); // NOI18N
        jSlider1.setMaximum(1000);
        jSlider1.setValue(500);
        jSlider1.setName("jSlider1"); // NOI18N
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validateNums(evt);
            }
        });
        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                validateNumsf(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
// TODO add your handling code here:
    this.updateValsFromBar();
}//GEN-LAST:event_jSlider1StateChanged

private void validateC(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validateC
// TODO add your handling code here:
    validateAll();
}//GEN-LAST:event_validateC

private void validateCf(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_validateCf
// TODO add your handling code here:
    validateAll();
}//GEN-LAST:event_validateCf

private void validateNums(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validateNums
// TODO add your handling code here:
    validateAll();
}//GEN-LAST:event_validateNums

private void validateNumsf(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_validateNumsf
// TODO add your handling code here:
    validateAll();
}//GEN-LAST:event_validateNumsf

private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
// TODO add your handling code here:
    this.pai.setSelId(myNum);
}//GEN-LAST:event_formMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
